package com.sa.app.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sa.app.R;
import com.sa.app.activities.ItemActivity;
import com.sa.app.objects.VirtualService;
import com.sa.app.utilities.AsynchronousInsertUse;
import com.sa.app.utilities.MyApplication;

public class VirtualServiceFragment extends Fragment implements
OnItemClickListener {
	private ListView lista;
	private Context cont;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.cont = inflater.getContext();
		View v = inflater.inflate(R.layout.virtual_service, container, false);
		lista = (ListView) v.findViewById(R.id.listViewVirtualSevice);
		lista.setAdapter(new MyAdapterVirtual(getActivity()
				.getApplicationContext(), ((ItemActivity) getActivity())
				.getItemActual().getNfcID()));
		lista.setOnItemClickListener(this);
		return v;
	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// con position podemos acceder al la posicion dentro de la lista
		// del adapter
		if (parent.getId() == R.id.listViewVirtualSevice) {
			VirtualService virtualServiceActual = (VirtualService) ((ListAdapter) lista
					.getAdapter()).getItem(position);
			// Añadir los extras en el ultimo parametro de esta linea
			new AsynchronousInsertUse(getActivity(), cont, getActivity()
					.getApplication(), ((ItemActivity) getActivity())
					.getItemActual().getNfcID(), MyApplication.VIRTUAL,
					virtualServiceActual.getServiceURL(), "").execute("");
		}
	}

	private class MyAdapterVirtual extends BaseAdapter {
		private List<VirtualService> list = new ArrayList<VirtualService>();
		private Context context;

		public MyAdapterVirtual(Context c, String NFCID) {

			list.addAll(((ItemActivity) getActivity()).getItemActual()
					.getVirtualServices());
			this.context = c;
		}

		public int getCount() {

			return list.size();
		}

		public Object getItem(int position) {

			return list.get(position);
		}

		public long getItemId(int position) {

			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				LayoutInflater mInflater = LayoutInflater.from(context);
				convertView = mInflater.inflate(R.layout.service_row, null);// listview_fila,
				// null);
			}

			// Llamamos a los recursos que vamos a utlizar
			TextView text = (TextView) convertView
					.findViewById(R.id.textViewServiceTitle);// TextView01);
			TextView text2 = (TextView) convertView
					.findViewById(R.id.textViewServiceDescription);// TextView02);

			text.setText(list.get(position).getNameID());
			text2.setText(list.get(position).getDescription());

			return convertView;
		}

	}

	// Esto hace que no cuelgue la aplicación al cambiar de activity utilizando
	// la libreria de compatibilidad
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		setUserVisibleHint(true);
	}
}
